package com.service.scheduling.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.MemberJAType;
import com.service.dao.mapper.JournalAccountMapper;
import com.service.dao.model.JournalAccount;
import com.service.dao.model.Member;
import com.service.dao.model.MemberOut;
import com.service.dao.po.MemberCache;
import com.service.scheduling.service.MemberJAService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:会员账户流水</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年09月24日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
public class MemberJAServiceImpl extends ServiceImpl<JournalAccountMapper, JournalAccount> implements MemberJAService {

    @Override
    public void getMemberIncome(Integer memberId, MemberCache.MemberCacheBuilder builder, MemberOut memberOut) {
        Date startTime = null, endTime = null;
        if (!ObjectUtils.isEmpty(memberOut)) {
            startTime = memberOut.getCreateTime();
            endTime = new Date();
        }
        List<JournalAccount> incomeList = baseMapper.listTotalAccumulatedIncome(memberId, MemberJAType.getAwardIncomeType(), startTime, endTime);
        List<JournalAccount> miningList = baseMapper.listTotalAccumulatedIncome(memberId, MemberJAType.getMiningIncomeType(), null, null);
        incomeList.addAll(miningList);

        if (ObjectUtils.isEmpty(incomeList)) {
            return;
        }

        for (JournalAccount item : incomeList) {
            switch (item.getJaType()) {
                case 11:
                    //直间奖
                    builder.directAward(item.getJaNum());
                    break;
                case 12:
                    //间接奖
                    builder.indirectAward(item.getJaNum());
                    break;
                case 13:
                    //社区奖
                    builder.communityAward(item.getJaNum());
                    break;
                case 14:
                    //节点
                    builder.nodeAward(item.getJaNum());
                    break;
                case 50:
                    //挖矿
                    builder.hashRateAward(item.getJaNum());
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public BigDecimal getMemberSomedayReleaseNum(Integer memberId, List<Integer> typeList, CoinsEnum coin, Date someday) {
        String somedayStr = DateFormatUtils.format(someday, DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.getPattern());

        return baseMapper.getSomedayTotalStatementByCoins(memberId, coin.getName(), typeList, somedayStr);

    }

    @Override
    public BigDecimal getMemberTeamSomedayReleaseNum(List<Member> memberList, List<Integer> typeList, CoinsEnum coin, Date someday) {
        String somedayStr = DateFormatUtils.format(someday, DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.getPattern());
        return baseMapper.getMemberTeamSomedayTotalStatementByCoins(memberList, coin.getName(), typeList, somedayStr);
    }

    /**
     * @param memberIds
     * @param currentMonth
     * @Description: 统计本月业绩 排除代理商和分公司伞下业绩
     * @Author: ty
     * @Date: 2021/5/6 15:36
     * @param: [memberIds, currentMonth]
     * @return: java.math.BigDecimal
     */
    @Override
    public BigDecimal totalPerformanceCurrentMonth(Set<Integer> memberIds, Date currentMonth) {
        String currentMonthStr = DateFormatUtils.format(currentMonth, "yyyy-MM");
        return baseMapper.totalPerformanceCurrentMonth(memberIds,MemberJAType.getReleaseType(),currentMonthStr);
    }

    /**
     * @param typeList
     * @param coin
     * @param someday
     * @Description: 统计全网某一天的所有释放数量
     * @Author: ty
     * @Date: 2021/4/28 11:10
     * @param: [typeList, coin, someday]
     * @return: java.math.BigDecimal
     */
    @Override
    public BigDecimal totalAllNetworkSomeDayReleaseNum(List<Integer> typeList, String coinsName, Date someday) {
        String somedayStr = DateFormatUtils.format(someday, DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.getPattern());
        return baseMapper.totalAllNetworkSomeDayReleaseNum(typeList, coinsName, somedayStr);
    }
}
